package com.ac.scum.log.web.model.user.controller;

import com.ac.common.core.exception.BaseException;
import com.ac.common.core.exception.config.ExceptionType;
import com.ac.common.core.result.Result;
import com.ac.scum.log.bean.AdminUser;
import com.ac.scum.log.web.dto.login.LoginDTO;
import com.ac.scum.log.web.model.user.service.UserService;
import com.ac.scum.log.web.utils.user.CurrentUserUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

/**
 * 用户
 */
@RestController
@RequestMapping("user")
@AllArgsConstructor
public class UserController {

    private final UserService userService;
    private final RedisTemplate<String, Object> redisTemplate;


    @GetMapping("info")
    public Result<Object> getCurrentLoginUserInfo() throws BaseException {
        return Result.ok(redisTemplate.opsForValue().get(CurrentUserUtil.getCurrentToken()));
    }

    @PostMapping("changePassword")
    public Result<Boolean> changePassword(@RequestParam("new") String newPassword, @RequestParam("old") String oldPassword) throws BaseException {
        return Result.ok(userService.changePassword(newPassword, oldPassword));
    }

    @PostMapping("registerSMS")
    public Result<Boolean> registerSendSMS(@RequestParam("phone") String phone) throws BaseException {
        if (userService.count(Wrappers.lambdaQuery(AdminUser.class).eq(AdminUser::getPhone, phone)) > 0) {
            throw new BaseException(ExceptionType.Has_Account);
        }

        return Result.ok(userService.sendSMS(phone));
    }

    @PostMapping("register")
    public Result<LoginDTO> registerUser(@RequestParam("phone") String phone, @RequestParam("code") String code) throws BaseException {
        return Result.ok(userService.register(phone, code));
    }
}
